# detection/model_menu.py
from PyQt5.QtWidgets import QAction, QFileDialog, QInputDialog
from PyQt5.QtCore import QObject, pyqtSignal

class ModelMenu(QObject):
    """
    模型菜单管理模块
    """
    load_model_signal = pyqtSignal(str)     # 载入模型路径
    select_model_signal = pyqtSignal(str)   # 从模型库选择模型
    set_params_signal = pyqtSignal(dict)    # 设置模型参数

    def __init__(self, parent):
        super().__init__(parent)
        self.parent = parent
        self._create_actions()

    def _create_actions(self):
        # 加载模型
        self.load_model_action = QAction("加载模型", self.parent)
        self.load_model_action.triggered.connect(self.load_model)

        # 模型库
        self.model_library_action = QAction("模型库", self.parent)
        self.model_library_action.triggered.connect(self.select_model_from_library)

        # 模型参数
        self.model_params_action = QAction("模型参数", self.parent)
        self.model_params_action.triggered.connect(self.set_model_params)

    def load_model(self):
        file, _ = QFileDialog.getOpenFileName(
            self.parent, "选择模型权重文件", "", "PyTorch Models (*.pt *.pth)"
        )
        if file:
            self.load_model_signal.emit(file)

    def select_model_from_library(self):
        # 示例：弹出列表选择已有模型
        models = ["yolov8n.pt", "yolov11n.pt", "yolov9c.pt"]
        model, ok = QInputDialog.getItem(self.parent, "选择模型", "模型库:", models, 0, False)
        if ok and model:
            self.select_model_signal.emit(model)

    def set_model_params(self):
        # 示例：设置参数，可扩展为弹出窗口
        params = {"conf": 0.25, "iou": 0.45, "device": "cpu"}
        self.set_params_signal.emit(params)

